package com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.casualtiesPE.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.ResponseCode;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.entity.Statistics;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.param.AssessParam;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.casualtiesPE.entity.CasualtiesPE;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.casualtiesPE.entity.CasualtiesPECalculation;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.casualtiesPE.param.CasualtiesPECalculationParam;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.casualtiesPE.service.CasualtiesPEService;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import com.css.fxfzfxqh.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/06/15
 * @Description: 人员伤亡概率评估
 */
@Slf4j
@RestController
@RequestMapping("/casualtiesPE")
public class CasualtiesPEController {

    @Resource
    private CasualtiesPEService casualtiesPEService;

    /**
     * 新增评估
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/addAssess")
    public RestResponse addAssess(@RequestBody CasualtiesPE param) {
        try {
            casualtiesPEService.addAssess(param);
            return RestResponse.succeed("新增成功");
        } catch (Exception e) {
            log.error("新增评估失败!", e);
            return RestResponse.fail("新增失败!");
        }
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id) {
        RestResponse response = null;
        try {
            CasualtiesPE vehManage=casualtiesPEService.findById(id);
            response = RestResponse.succeed(vehManage);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 修改评估
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/upDateAssess")
    public RestResponse upDateAssess(@RequestBody CasualtiesPE param) {
        try {
            casualtiesPEService.upDateAssess(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改评估失败!", e);
            return RestResponse.fail("修改失败!");
        }
    }

    /**
     * 获取评估code
     */
    @PostMapping("/getCode")
    public RestResponse getCode(@RequestBody CasualtiesPE param) {
        try {
            String code = casualtiesPEService.getCode(param);
            return RestResponse.succeed(code);
        } catch (Exception e) {
            String errormessage = "生成code失败";
            log.error(errormessage, e);
            return RestResponse.fail("获取失败");
        }

    }

    /**
     * 分页列表
     *
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, AssessParam param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = casualtiesPEService.getPage(curPage, pageSize, param);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 计算
     *
     * @param
     * @return
     */
    @PostMapping("/calculation")
    public RestResponse calculation(@RequestBody Map<String, Object> param) {
        try{
            casualtiesPEService.calculation((String)param.get("taskId"),(String)param.get("year"));
            return ResponseCreator.createSuccessResponse("评估成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseCreator.createFailResponse(e.getMessage(), ResponseCode.EVALUATE_ERROR);
        }


    }

    /**
     * 导入
     *
     * @param type 评估区域 1省市县2圈定区域
     * @param id   评估记录id
     * @param years   T年
     * @param code   评估记录code
     * @return
     */
    @PostMapping("/importExcel")
    public RestResponse reportImport(HttpServletRequest req, String type, String id,String years,String code) {
        RestResponse response = null;
        try {
            //获取上传到服务器的临时文件对象
            Part part = req.getPart("file");
            //获取临时文件的输入流
            InputStream inputStream = part.getInputStream();
            String str = casualtiesPEService.reportImport(inputStream, type, id,years,code);
            response = RestResponse.succeed(str);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 导出
     *
     * @param param 参数
     */
    @PostMapping("/exportData")
    public void exportData(HttpServletResponse response, CasualtiesPECalculationParam param) {
        casualtiesPEService.exportData(response, param);
    }

    /**
     * 查询计算值
     *
     * @param id               评估记录id
     * @param years               T年
     * @param casualties     人员伤亡(死亡重伤) 1 重伤 2死亡
     * @param time     时间段(白天晚上) 白天1 晚上0
     * @return
     */
    @GetMapping("/queryCalculation")
    public RestResponse queryCalculation( HttpServletRequest request,String id, String casualties,String time,String years) {
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> data = casualtiesPEService.queryCalculation( curPage,pageSize,id,casualties,time,years);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 修改概率P(t)修正值
     *
     * @return
     */
    @PostMapping("/updateCorrectionValue")
    public RestResponse updateCorrectionValue(@RequestBody CasualtiesPECalculation param) {
        RestResponse restResponse = null;
        try {
            casualtiesPEService.updateCorrectionValue(param);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }


    /**
     * 查询计算值
     * @param taskId               评估记录id
     * @param casualties     人员伤亡(死亡重伤)
     * @param time     时间段(白天晚上)
     * @return
     */
    @GetMapping("/statistics")
    public RestResponse statistics(String taskId, String casualties,String time) throws Exception{
        List<Statistics> list = casualtiesPEService.statistics(taskId,casualties,time);
        return ResponseCreator.createSuccessResponse(list);
    }


}
